﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Net.Chirash.Twitter.TwitterDatabase;

namespace Net.Chirash.Twitter.TwitterClient
{
    public class TwitterMessage
        : TwitterData
    {
        public TwitterMessageType MessageType { get; set; }
        public TwitterData Data { get; set; }
    }

    public class TwitterEvent
        : TwitterData
    {
        public TwitterEventType EventType { get; set; }
        public User Target { get; set; }
        public TwitterData TargetObject { get; set; }
        public User Source { get; set; }
        public DateTime CreatedAt { get; set; }

        #region Parse

        public static TwitterEvent Parse(string xml, ITwitterCommand cmd)
        {
            var element = XElement.Parse(xml);
            var xmlTarget = element.Element("target").Value;
            var xmlSource = element.Element("source").Value;

            var xmlElementTargetObject = element.Element("target_object"); //target_objectは必ず存在するわけじゃない
            string xmlTargetObject = "";
            if (xmlElementTargetObject != null)
                xmlTargetObject = xmlElementTargetObject.Value;

            var result = new TwitterEvent();
            result.CreatedAt = DateTime.ParseExact(element.Element("created_at").Value, "ddd MMM d HH':'mm':'ss zzz yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo); ;
            result.Source = new User().Parse(xmlSource);
            result.Target = new User().Parse(xmlTarget);

            switch (element.Element("event").Value)
            {
                case "list_member_added":
                    result.EventType = TwitterEventType.ListMemberAdded;
                    result.TargetObject = new TwitterList().Parse(xmlTargetObject);
                    break;
                case "list_member_removed":
                    result.EventType = TwitterEventType.ListMemberRemoved;
                    result.TargetObject = new TwitterList().Parse(xmlTargetObject);
                    break;
                case "favorite":
                    result.EventType = TwitterEventType.Favorited;
                    result.TargetObject = new Status().Parse(xmlTargetObject, cmd);
                    break;
                case "unfavorite":
                    result.EventType = TwitterEventType.UnFavorited;
                    result.TargetObject = new Status().Parse(xmlTargetObject, cmd);
                    break;
                case "follow":
                    result.EventType = TwitterEventType.Followed;
                    result.TargetObject = null;
                    break;
                case "list_created":
                    result.EventType = TwitterEventType.ListCreated;
                    result.TargetObject = new TwitterList().Parse(xmlTargetObject);
                    break;
                case "list_updated":
                    result.EventType = TwitterEventType.ListUpdated;
                    result.TargetObject = new TwitterList().Parse(xmlTargetObject);
                    break;
                case "list_destroyed":
                    result.EventType = TwitterEventType.ListDestroyed;
                    result.TargetObject = null;// TwitterList.Parse(xmlTargetObject);
                    break;
                case "block":
                    result.EventType = TwitterEventType.Blocked;
                    result.TargetObject = null;
                    break;
                case "unblock":
                    result.EventType = TwitterEventType.UnBlocked;
                    result.TargetObject = null;
                    break;
                default:
                    Console.WriteLine("TwitterMessage : unknown event {0}", xml);
                    result.EventType = TwitterEventType.Unknown;
                    result.TargetObject = null;
                    break;
            }
            return result;
        }

        #endregion
    }

    public enum TwitterMessageType
    {
        Status,
        RetweetedStatus,
        DirectMessage,
        DeleteStatus,
        CurrentFriends,
        Event,
        Unknown,
    }

    public enum TwitterEventType
    {
        ListMemberAdded,
        ListMemberRemoved,
        Followed,
        Favorited,
        UnFavorited,
        ListCreated,
        ListUpdated,
        ListDestroyed,
        Blocked,
        UnBlocked,
        Unknown
    }
}
